package HashMap源码;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class ErgodicDemo {

    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String, String>();
        map.put("1","value1");
        map.put("2","value2");
        map.put("3","value3");
        map.put("4","value4");

        //第一种 通过Map.entrySet遍历，推荐使用，尤其容量大时
        for (Map.Entry<String, String> stringStringEntry : map.entrySet()) {
            System.out.println("key:"+stringStringEntry.getKey()+"-value:"+stringStringEntry.getValue());
        }

        //第二种 通过Map.entrySet使用iterator遍历
        Iterator mapplit = map.entrySet().iterator();
        while (mapplit.hasNext()){
            Map.Entry<String,String> entry = (Map.Entry<String, String>) mapplit;
            System.out.println("key:"+entry.getKey()+"-value:"+entry.getValue());
        }

        //第三种 通过Map.keySet遍历，二次取值
        for (String s : map.keySet()) {
            System.out.println("key:"+s+"-value:"+map.get(s));
        }

        //第四种 通过Map.value()遍历
        for (String s : map.values()) {
            System.out.println("The value is"+s);
        }
    }
}
